﻿using System;
using System.Collections.Generic;
using System.Linq;

// 订单明细类
class OrderDetails
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }

    public override string ToString()
    {
        return $"{ProductName} (Qty: {Quantity}, Price: {UnitPrice:C})";
    }

    public override bool Equals(object obj)
    {
        if (obj is OrderDetails other)
        {
            return ProductId == other.ProductId;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return ProductId.GetHashCode();
    }
}

// 订单类
class Order
{
    public int OrderId { get; set; }
    public string CustomerName { get; set; }
    public List<OrderDetails> OrderItems { get; } = new List<OrderDetails>();

    public decimal TotalAmount => OrderItems.Sum(item => item.Quantity * item.UnitPrice);

    public override string ToString()
    {
        return $"Order {OrderId} for {CustomerName} (Total Amount: {TotalAmount:C})";
    }

    public override bool Equals(object obj)
    {
        if (obj is Order other)
        {
            return OrderId == other.OrderId;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return OrderId.GetHashCode();
    }
}

// 订单服务类
class OrderService
{
    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (orders.Contains(order))
        {
            throw new InvalidOperationException("Order already exists.");
        }
        orders.Add(order);
    }

    public void RemoveOrder(int orderId)
    {
        var orderToRemove = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (orderToRemove == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        orders.Remove(orderToRemove);
    }

    // 其他方法：ModifyOrder、QueryOrders、SortOrders 等

    // 使用 LINQ 查询功能
    public IEnumerable<Order> QueryOrders(Func<Order, bool> query)
    {
        return orders.Where(query);
    }

    // 排序方法
    public void SortOrders(Func<IEnumerable<Order>, IOrderedEnumerable<Order>> sortFunc)
    {
        orders = sortFunc(orders).ToList();
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 初始化 OrderService，处理用户输入等
        // 示例代码中只展示了部分功能，您可以根据需求完善其他功能。
    }
}
